package com.lottery.system.controller.handler;

import com.lottery.system.common.errorcode.GlobeErrorCodeConstants;
import com.lottery.system.common.exception.ControllerException;
import com.lottery.system.common.exception.ServiceException;
import com.lottery.system.common.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: xiaoxie
 * create: 2024-09-02 21:02
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.controller.handler
 * description: 全局异常捕获
 */

//全局异常捕获的注解
@RestControllerAdvice
public class GlobalExceptionHandler {
    //日志
    private final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    /**
     * @description: 业务逻辑层返回异常的捕获
     * @author: xiaoxie
     * @date: 2024/9/2 21:07
     * @param: [e]
     * @return: com.lottery.system.common.pojo.CommonResult<?>
     **/
    @ExceptionHandler(ServiceException.class)
    public CommonResult<?> handleServiceException(ServiceException e) {
        //日志打印
        logger.error("ServiceException", e);
        //封装返回结果
        return CommonResult.fail(GlobeErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode()
                ,e.getMsg());
    }
    /**
     * @description: 控制层返回异常的捕获
     * @author: xiaoxie
     * @date: 2024/9/2 21:07
     * @param: [e]
     * @return: com.lottery.system.common.pojo.CommonResult<?>
     **/
    @ExceptionHandler(ControllerException.class)
    public CommonResult<?> handleControllerException(ControllerException e) {
        //日志打印
        logger.error("ControllerException: ", e);
        //封装返回结果
        return CommonResult.fail(GlobeErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode()
                ,e.getMsg());
    }
    /**
     * @description: 未知异常的捕获
     * @author: xiaoxie
     * @date: 2024/9/2 21:07
     * @param: [e]
     * @return: com.lottery.system.common.pojo.CommonResult<?>
     **/
    @ExceptionHandler(Exception.class)
    public CommonResult<?> handleControllerException(Exception e) {
        //日志打印
        logger.error("Exception: ", e);
        //封装返回结果
        return CommonResult.fail(GlobeErrorCodeConstants.INTERNAL_SERVER_ERROR);
    }

}
